package com.ply.flashsalessystem.config;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.ply.flashsalessystem.exception.FileException;
import com.ply.flashsalessystem.exception.GoodsException;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.exception.IdentityException;
import com.ply.flashsalessystem.exception.OrderException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
3
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        log.debug(e.getMessage());
        return Result.error().message(e.getMessage());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public Result HttpMessageNotReadableException(Exception e){
        log.debug(e.getMessage());
        return Result.error().message(e.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result error1(Exception e){
        log.debug(e.getMessage());
        return Result.error().message(e.getMessage());
    }


    @ExceptionHandler(GoodsException.class)
    @ResponseBody
    public Result GoodsException(GoodsException e){
        log.debug(e.getMessage());
       return new Result(false, 20100, e.getMessage(), null);
    }


    @ExceptionHandler(OrderException.class)
    @ResponseBody
    public Result OrderException(OrderException e){
        log.debug(e.getMessage());
        return new Result(false, 20200, e.getMessage(), null);
    }

    @ExceptionHandler(IdentityException.class)
    @ResponseBody
    public Result IdentityException(IdentityException e){
        log.debug(e.getMessage());
        return new Result(false, 30000, e.getMessage(), null);
    }

    @ExceptionHandler(FileException.class)
    @ResponseBody
    public Result FileException(FileException e){
        log.debug(e.getMessage());
        return new Result(false, 30100, e.getMessage(), null);
    }
}